package com.hk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hk.reggie.common.R;
import com.hk.reggie.entity.Category;
import com.hk.reggie.entity.Dish;
import com.hk.reggie.entity.DishFlavor;
import com.hk.reggie.service.CategoryService;
import com.hk.reggie.service.DishFlavorService;
import com.hk.reggie.service.DishService;
import com.hk.reggie.vo.DishVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("status/{status}")
    public R<String> status(@PathVariable("status") Integer status,@RequestParam List<Long> ids){
        List<Dish> dishList = dishService.list(new QueryWrapper<Dish>().in("id", ids));
        for(Dish dish : dishList) {
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        dishService.removeWithDish(ids);

        return R.success("删除成功");
    }

    /**
     * 修改菜品
     * @param dishVo
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishVo dishVo){
        dishService.updateWithFlavor(dishVo);
        return R.success("修改菜品成功");
    }

    /**
     * 新增菜品
     * @param dishVo
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishVo dishVo){
        dishService.saveDishInfo(dishVo);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构建分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishVo> dishVoPage = new Page<>(page,pageSize);

        //条件构造器
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        if(name != null){
            queryWrapper.like("name", name);
        }
        queryWrapper.orderByDesc("update_time");

        //执行分页查询
        dishService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dishVoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        List<DishVo> list = records.stream().map(record -> {
            DishVo dishVo = new DishVo();
            //拷贝数据
            BeanUtils.copyProperties(record, dishVo);
            //通过分类ID查询出分类名，被赋值给DishVo
            Long categoryId = dishVo.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                dishVo.setCategoryName(category.getName());
            }
            return dishVo;
        }).collect(Collectors.toList());

        dishVoPage.setRecords(list);

        return R.success(dishVoPage);
    }

    /**
     * 根据Id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishVo> getDishById(@PathVariable("id") Long id){
        DishVo dishVo = dishService.getByIdWithFlavor(id);
        return R.success(dishVo);
    }

//    /**
//     * 根据条件来查询菜品数据
//     * @param dish
//     * @return
//     */
//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        //构造查询条件
//        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("category_id", dish.getCategoryId()).eq("status", 1);
//        //添加排序条件
//        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
//
//        List<Dish> list = dishService.list(queryWrapper);
//        return R.success(list);
//    }

    /**
     * 根据条件来查询菜品数据
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishVo>> list(Dish dish){
        //构造查询条件
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", dish.getCategoryId()).eq("status", 1);
        //添加排序条件
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");

        List<DishVo> list = dishService.list(queryWrapper).stream().map((item) -> {
            Long dishId = item.getId();
            List<DishFlavor> flavors = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", dishId));
            DishVo dishVo = new DishVo();
            BeanUtils.copyProperties(item, dishVo);
            dishVo.setFlavors(flavors);
            return dishVo;
        }).collect(Collectors.toList());
        return R.success(list);
    }

}























